package com.zeny.coding.service.impl;


import com.zeny.coding.constants.CourseConstants;
import com.zeny.coding.dto.CategoryDto;
import com.zeny.coding.entity.Category;
import com.zeny.coding.entity.CategoryExample;
import com.zeny.coding.mapper.CategoryMapper;
import com.zeny.coding.service.CategoryService;
import com.zeny.coding.util.SnowFlake;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <h2>分类服务</h2>
 *
 * @Author Zeny
 */
@SuppressWarnings("all")
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<CategoryDto> list(String parent) {
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andParentEqualTo(parent);
        categoryExample.setOrderByClause("sort asc");
        List<Category> categoryList = this.categoryMapper.selectByExample(categoryExample);
        List<CategoryDto> categoryDtoList = new ArrayList<>();
        for (Category category : categoryList) {
            CategoryDto categoryDto = new CategoryDto();
            BeanUtils.copyProperties(category, categoryDto);
            categoryDtoList.add(categoryDto);
        }
        return categoryDtoList;
    }

    @Override
    public void save(CategoryDto categoryDto) {
        //DTO转~
        Category category = new Category();
        BeanUtils.copyProperties(categoryDto, category);
        //算法算法生成ID
        SnowFlake snowFlake = new SnowFlake(
                CourseConstants.dataCenterId,
                CourseConstants.machineId);
        category.setId(String.valueOf(snowFlake.nextId()));
        this.categoryMapper.insertSelective(category);
    }

    @Override
    public void update(CategoryDto categoryDto) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDto, category);
        this.categoryMapper.updateByPrimaryKeySelective(category);
    }

    @Override
    public CategoryDto findById(String id) {
        Category category = this.categoryMapper.selectByPrimaryKey(id);
        CategoryDto categoryDto = new CategoryDto();
        BeanUtils.copyProperties(category, categoryDto);
        return categoryDto;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteById(String id) {
        //查询以当前节点为父节点的节点
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andParentEqualTo(id);
        List<Category> list = this.categoryMapper.selectByExample(categoryExample);
        if (list != null && list.size() > 0) {
            for (Category category : list) {
                deleteById(category.getId());
            }
        }
        this.categoryMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<CategoryDto> findAll() {
        List<Category> categoryList = this.categoryMapper.selectByExample(null);
        List<CategoryDto> categoryDtoList = new ArrayList<>();
        for (Category category : categoryList) {
            CategoryDto categoryDto = new CategoryDto();
            BeanUtils.copyProperties(category, categoryDto);
            categoryDtoList.add(categoryDto);
        }
        return categoryDtoList;
    }

    @Override
    public Map<String, List<CategoryDto>> findMapAll() {
        List<Category> categories = this.categoryMapper.selectByExample(null);
        //构建索引 key: 分类id, value: CategoryDto
        Map<String, CategoryDto> map = new HashMap<>();
        for (Category category : categories) {
            CategoryDto categoryDto = new CategoryDto();
            BeanUtils.copyProperties(category, categoryDto);
            map.put(categoryDto.getId(), categoryDto);
        }
        //所有的二级菜单
        List<CategoryDto> level2 = new ArrayList<>();
        List<CategoryDto> categoryDtos = new ArrayList<>();
        //构造树形菜单
        for (Category category : categories) {
            String pid = category.getParent();
            CategoryDto categoryDto = map.get(category.getId());
            if ("00000000".equals(pid)) {
                //当前节点是一级节点
                categoryDtos.add(categoryDto);
            } else {
                //非一级节点
                CategoryDto parent = map.get(pid);
                if (parent != null) {
                    parent.getChildren().add(categoryDto);
                    level2.add(categoryDto);
                }
            }
        }
        Map<String, List<CategoryDto>> result = new HashMap<>();
        result.put("all", categoryDtos);
        result.put("level2", level2);
        return result;
    }
}
